/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QUT.Bio.Graphs;
using QUT.Bio.Map2D;
using QUT.Bio.Util;
using QUT.Bio.SilverMap.ObjectModel;

// TODO: Review -- perhaps overlay should be removed from here, instead provided by the container.

namespace QUT.Bio.SilverMap {
	public partial class View : UserControl {
		SequenceGraphView graphView = new SequenceGraphView();

		public View () {
			InitializeComponent();

			graphView.Canvas.ShowStatus = false;
			//graphView.Canvas.LayoutBackground = (Brush) Resources["BG_COLOR_BRUSH"];
			graphViewHolder.Children.Add( graphView.Canvas );
			
			arrowPad.ActionEvent += arrowPad_ActionEvent;
		}

		private const double PAN = 10;
		private const int TOOL_PADDING = 5;

		private readonly Dictionary<int, Action> toolActions = new Dictionary<int, Action>();

		private void SelectClicked ( object sender, RoutedEventArgs args ) {
			if ( model.ReferenceSequence != null ) {
				foreach ( SequenceSummary summary in model.SequenceSummaries ) {
					if ( summary.DistanceFromReferenceSequence <= threshold ) {
						summary.Selected = true;
					}
				}
			}
		}

		private void DeselectClicked ( object sender, RoutedEventArgs args ) {
			if ( model.ReferenceSequence != null ) {
				foreach ( SequenceSummary summary in model.SequenceSummaries ) {
					if ( summary.DistanceFromReferenceSequence <= threshold ) {
						summary.Selected = false;
					}
				}
			}
		}

		private void arrowPad_ActionEvent ( ArrowPad.Action action ) {
			switch ( action ) {
				case ArrowPad.Action.Left:
					graphView.Canvas.Pan( -PAN, 0 );
					break;
				case ArrowPad.Action.Right:
					graphView.Canvas.Pan( +PAN, 0 );
					break;
				case ArrowPad.Action.Up:
					graphView.Canvas.Pan( 0, -PAN );
					break;
				case ArrowPad.Action.Down:
					graphView.Canvas.Pan( 0, +PAN );
					break;
				case ArrowPad.Action.Reset:
					graphView.Canvas.X = 0;
					graphView.Canvas.Y = 0;
					graphView.Canvas.Scale = 1;
					break;
				default:
					break;
			}
		}

		private void InitializeZoomSlider () {
			graphView.Canvas.MinimumScale = 0.1;
			graphView.Canvas.MaximumScale = 8;
			zoomSlider.Label = "Zoom:";
			zoomSlider.ValueConverter = delegate( double v ) {
				return ( (int) Math.Round( v * 100 ) ).ToString() + "%";
			};
			zoomSlider.Bind( graphView.Canvas, "Scale", "MinimumScale", "MaximumScale" );
		}

		/// <summary>
		/// Gets a reference to the map canvas.
		/// </summary>

		public SequenceGraphView GraphView {
			get {
				return graphView;
			}
		}

		/// <summary>
		/// Gets a reference to the threshold slider.
		/// </summary>

		public LabelledSlider ThresholdSlider {
			get {
				return this.thresholdSlider;
			}
		}

		/// <summary>
		/// Gets a reference to the stretch slider.
		/// </summary>

		public LabelledSlider StretchSlider {
			get {
				return this.stretchSlider;
			}
		}
	}
}
